package lty.pritace.queue;

import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 点对点推送消息
 * 
 * @author lty
 *
 */
public class QueueReceiver {
	public static void main(String[] args) throws Exception {
		ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://192.168.233.128:61616");
		// new ActiveMQConnectionFactory("tcp://192.168.233.128:61616");
		Connection connection = cf.createConnection();
		connection.start();
		Enumeration names = connection.getMetaData().getJMSXPropertyNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			System.out.println("jmx name===" + name);
		}
		final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue("my-queue");
		MessageConsumer consumer = session.createConsumer(destination);
		int i = 0;
		while (i < 3) {
			// TextMessage message = (TextMessage) consumer.receive();
			MapMessage message = (MapMessage) consumer.receive();
			session.commit();
			System.out.println("收到消息：" + message.getString("message" + i) + ",property=="
					+ message.getStringProperty("EXTRAS" + i));
			i++;
		}
		session.close();
		connection.close();
	}
}
